#include "stdafx.h"
#include "IOStation.h"


IOStation::IOStation(Distribution* dist, int itemCount) : Station(0, dist, NULL), itemCount(itemCount),	currentItem(1)
{
	stats = new Statistics();
}

IOStation::~IOStation(void)
{
}

void IOStation::timeTrigger(double time, Scheduler* scheduler) {

	if(currentItem>itemCount) {
		return;
	};

	scheduler->addTrigger(time + distribution->generate(), this);

	double nextRand = (double)rand()/RAND_MAX;
	double chanceSum = 0;
	int sId=-1;
	do {
		chanceSum+=nextStations[++sId].second;
	} while(chanceSum<nextRand);

	if(nextStations[sId].first->putItem(pair<double, int>(time, currentItem++), this, scheduler)) {
		stats->itemIn(currentItem-1, time);
		cout<<"Wszedl do systemu: "<<currentItem-1<<endl;
	} else {
		stats->itemRejected();
		cout<<"Odrzucony: "<<currentItem-1<<endl;
	}
}

bool IOStation::putItem(pair<double, int> item, Station* fromStation, Scheduler* scheduler) {
	stats->itemOut(item.second, item.first);
	cout<<"Wyszedl z systemu: "<<item.second<<endl;
	return true;
}